<template>
	<view class="content">
		<view class="top-menu r-nw-sa-c">
			<view class="item c-c-c" v-for="item in menus" :key="item.id" @click="changeMenu(item.id)">
				<view class="title" :style="{'color': item.id === currentMenu ? '#EC2F12' : ''}">{{item.title}}</view>
				<image style="width: 32rpx;height: 12rpx;" v-if="item.id === currentMenu" src="../../static/image/icon1.png" mode="aspectFit"></image>
				<view style="width: 32rpx;height: 12rpx;" v-else></view>
			</view>
		</view>
		<scroll-view scroll-y="true" class="scroll" @scrolltolower="scrolltolower">
			<view v-if="orderList.length > 0">
				<view class="order-item" v-for="item in orderList" :key="item.id">
					<view class="order-item-status r-nw-sb-c">
						<text>下单时间：{{item.date}}</text>
						<text class="statu" :style="{'color': item.is_delivery === 0 ? '#FF5C1F' : '#737582'}">{{item.is_delivery === 0 ? '待交车' : '已完成'}}</text>
					</view>
					<view class="title">{{item.order_car.name}}</view>
					<view class="config">{{item.order_car.describe}}</view>
					<view class="times-total-num">
						<view class="time r-nw-sb-c">取车时间：<text class="pick-up">{{item.start_date}}</text> 
						</view>
						<view class="time r-nw-sb-c">取车地点：<text class="pick-up">{{item.city}} {{item.address}}</text></view>
						<view class="time r-nw-sb-c">联系人：<text class="pick-up">{{item.order_lessee.name}}&nbsp;&nbsp;{{item.order_lessee.phone}}</text></view>
						
					</view>
					<view class="price-btns r-nw-fe-c">
						<view class="btn-details btn c-c-c" @click="goToDetail(item.id)">
							查看详情
						</view>
						<view class="btn-payment btn c-c-c btn-call" v-if="item.is_delivery === 0"  @click="goToCallUp(item.order_lessee.phone)">
							联系客户
						</view>
						<view class="btn-payment btn c-c-c" v-if="item.is_delivery === 0" @click="goToDelivery(item.id)">
							确认送达
						</view>
					</view>
				</view>
			</view>
			<view class="none-data" v-else>
				暂无数据~
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { orders, ordersDetail, delivery } from '@/utils/api.js';
	export default {
		data() {
			return {
				menus: [
					{
						id: 0,
						title: '待交车'
					},
					{
						id: 1,
						title: '已完成'
					},
				],
				currentPage: 1,
				currentMenu: 0,
				orderList: []
			}
		},
		onLoad() {
			this.changeMenu(0);
		},
		methods: {
			// 下拉加载
			scrolltolower() {
				this.currentPage++;
				this.getList();
			},
			// 查看详情
			goToDetail(id) {
				uni.navigateTo({
					url:'/pages/order/orderDetail?id=' + id
				})
			},
			// 切换菜单
			changeMenu(id) {
				this.currentPage = 1;
				this.orderList = [];
				// uni.setStorageSync('currentOrderMenu',id);  
				this.currentMenu = id;
				this.getList();
			},
			// 获取列表
			getList() {
				uni.showLoading({
					title: '加载中...'
				})
				orders({is_delivery: this.currentMenu, page: this.currentPage}).then(res => {
					if(res.code === 200) {
						this.orderList = this.orderList.concat(res.data.list);
						uni.hideLoading({})
					}
				})
				.catch(err => {})
			},
			// 打电话
			goToCallUp(tel) {
				uni.makePhoneCall({
					phoneNumber: tel
				})
			},
			// 确认送达
			goToDelivery(id) {
				let that = this;
				uni.showModal({
						content: '是否确认送达 ？',
						success: function(res) {
						if (res.confirm) {
							delivery(id).then(res => {
								if(res.code === 200) {
									uni.showToast({title: '送达成功'})
									that.changeMenu(0);
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F5F6F8;
	}
	.scroll {
		margin-top: 112rpx;
		height: 90vh;
		padding-bottom: 30rpx;
	}
	.none-data {
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		letter-spacing: 3rpx;
		color: #666;
	}
	.top-menu {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		width: 750rpx;
		height: 88rpx;
		background: #FFFFFF;
		margin-bottom: 4rpx;
		.title {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
			line-height: 28rpx;
			margin-bottom: 9rpx;
		}
	}
	.order-item {
		margin: 20rpx auto 0;
		padding: 24rpx;
		width: 638rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
	}
	.order-item-status {
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 26rpx;
		color: #999999;
		line-height: 40rpx;
		.statu {
			font-weight: 500;
			color: #FF5C1F;
		}
	}
	.title {
		margin-top: 24rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 600;
		font-size: 32rpx;
		color: #333333;
	}
	.config {
		margin-top: 16rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 24rpx;
		color: #666666;
		line-height: 32rpx;
	}
	.times-total-num {
		margin-top: 32rpx;
		.time {
			margin-bottom: 10rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #666666;
			line-height: 40rpx;
		}
		.pick-up {
			display: inline-block;
			width: 490rpx;
		}
	}
	.price-btns {
		margin-top: 32rpx;
		.btn {
			width: 160rpx;
			height: 60rpx;
			border-radius: 30rpx;
			border: 2rpx solid #EC2F12;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #FF0E25;
			line-height: 28rpx;
		}
		.btn-call {
			border: 2rpx solid #FF5C1F;
			color: #FF5C1F;
			margin: 0 20rpx;
		}
		.btn-details {
			border: 2rpx solid #999999;
			color: #666666;
		}
	}
</style>
